package com.jl.card;

import opencard.core.terminal.CommandAPDU;
import opencard.core.terminal.ResponseAPDU;

import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;
import org.mozilla.javascript.annotations.JSGetter;

public class CardJSProxy extends ScriptableObject implements ICard{

	private Card card;

	public CardJSProxy() {
	}

	@JSConstructor
	public CardJSProxy(String name) {
		card = CardManager.getInstance().getCardInstanceByName(name);
		if (card == null) {
			card = new Card(name);
		}
	}

	@Override
	@JSGetter
	public String getName() {
		return card.getName();
	}

	@Override
	@JSFunction
	public boolean open() {
		return card.open();
	}

	@Override
	@JSFunction
	public boolean close() {
		return card.close();
	}

	@Override
	public boolean reconnect() {
		return card.reconnect();
	}

	@Override
	@JSFunction
	public byte[] plainAPDU(String cmd) {
		return card.plainAPDU(cmd);
	}

	@Override
	@JSFunction
	public byte[] reset(boolean worm) {
		return card.reset(worm);
	}
	
	@Override
	@JSFunction
	public boolean verify(int type,String value){
		return card.verify(type, value);
	}
	
	public Card getInnerCard(){
		return card;
	}
	
	@Override
	public String toString() {
		return "[object Card]";
	}
	@Override
	public String getClassName() {
		return "Card";
	}

	@Override
	public void setProvider(String provider)
			throws ReflectiveOperationException {
		card.setProvider(provider);
	}

	@Override
	public ResponseAPDU sendCommandAPDU(CommandAPDU capdu) {
		return card.sendCommandAPDU(capdu);
	}

	@Override
	public boolean isConnect() {
		return card.isConnect();
	}

}
